package com.zsrd.self.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsrd.self.base.Result;
import com.zsrd.self.entity.OrderDiscounts;
import com.zsrd.self.entity.SelfUser;
import com.zsrd.self.service.OrderDiscountsService;
import com.zsrd.self.service.SelfUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/official")
public class OfficialController {


    @Autowired
    private OrderDiscountsService orderDiscountsService;

    @Autowired
    private SelfUserService selfUserService;

    // 返回支付状态 type = 1 支付 type = 2 未支付
    @PostMapping("/state")
    public Result state(@RequestBody OrderDiscounts param) {

        if (!StringUtils.isEmpty(param.getOpenId())) {
            // 前端传openid user表 没有返回3
            SelfUser user = selfUserService.selectType(param);
            if (StringUtils.isEmpty(user)) {
                param.setType("3");
                return Result.success(param);
            } else {
                OrderDiscounts type = orderDiscountsService.selectState(param);
                if (!StringUtils.isEmpty(type)) {
                    type.setType("1");
                    return Result.success(type);
                } else if (StringUtils.isEmpty(type)) {
                    param.setType("2");
                    return Result.success(param);
                }

            }
        } else if (StringUtils.isEmpty(param.getOpenId())) {
            param.setType("2");
            return Result.success(param);
        }
        return Result.success("成功");
    }


    @PostMapping("/isUserInfo")
    public Result type(@RequestBody SelfUser selfUser) {
        LambdaQueryWrapper<SelfUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SelfUser::getAppletId , selfUser.getAppletId());
        wrapper.isNotNull(SelfUser::getUserName);
        SelfUser user = selfUserService.getOne(wrapper);
        if (Objects.isNull(user)){
            // 2 没填写用户信息
            selfUser.setState("2");
            return Result.success(selfUser);
        }else {
            // 1是填写了用户信息
            user.setState("1");
            return Result.success(user);
        }
    }
}
